<?php
namespace App\Console\Commands;

use App\Jobs\Elesticsearch;
use App\Models\Media;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Redis;
use App\Models\ES\User AS esUser;
use App\Models\ES\Media AS esMedia;

class sync_es_search extends Command {

    /**
     * @var 设置执行的命令
     */
    protected $signature  = 'sync_es_search:send {action}';

    /**
     * The console command description.
     *
     * @var string
     */

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 获取参数，调用方法
        $action = $this->argument('action');
        $this->$action();

        //这里做任务的具体处理，可以用模型
        // Log::info('任务调度'.time());
    }

    /*
     * 同步批量添加用户 / 视频
     * */
    public function sync_user_media(){
        $userList = User::select(['id', 'name', 'mobile'])->get()->toArray();
        foreach ($userList as $key=>$value){
            // 队列   --添加搜索索引
            $params['uid'] = $value['id'];
            $params['name'] = $value['name'];
            $params['mobile'] = $value['mobile'];
            dispatch((new Elesticsearch(['type'=>'1', 'action'=>'add', 'data'=>$params]))->onQueue('eleasticsearch'));
        }
        $mediaList = Media::select(['id', 'title', 'description', 'cover', 'is_private'])->where([['status', '=', '1'], ['is_delete', '=', '0']])->get()->toArray();
        foreach ($mediaList as $k=>$v){
            if($v['is_private'] == '1')
                continue;
            $temp_arr['mid'] = $v['id'];
            $temp_arr['title'] = $v['title'];
            $temp_arr['description'] = $v['description'];
            $temp_arr['cover'] = $v['cover'];
            dispatch((new Elesticsearch(['type'=>'2', 'action'=>'add', 'data'=>$temp_arr]))->onQueue('eleasticsearch'));
        }
    }

}